package com.asset.dispoal.verdict.service.common;

import com.asset.dispoal.verdict.domain.db.entity.GbJudgmentBook;
import com.asset.dispoal.verdict.domain.db.mapper.GbJudgmentBookMapper;
import com.asset.dispoal.verdict.dto.request.CreateOrEditRequest;
import com.asset.dispoal.verdict.service.converter.GbJudgmentBookConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.Date;

/**
 * @description:
 * @author: yf
 * @date: 2022-09-06
 */
@Repository
public class EditOrCreateCommand implements BaseCommand<CreateOrEditRequest, Result> {
    @Autowired
    private BaseIdeable<String> snowFlake;
    @Autowired
    private GbJudgmentBookMapper judgmentBookMapper;

    @Override
    public Result execute(CreateOrEditRequest request) {
        GbJudgmentBook judgmentBook = GbJudgmentBookConverter.INSTANCE.reqToDo(request);
        String userId = GlobalHeaderThreadLocal.getOrException().getUserId();
        //判断id是否为空，为空则新增
        if (StringUtil.isBlank(judgmentBook.getId())) {
            judgmentBook.setId(snowFlake.generateId());
            judgmentBook.setCreateTime(new Date());
            judgmentBook.setCreateUserId(userId);
        }
        int i = judgmentBookMapper.insertOrUpdate(judgmentBook);
        return i <= 0 ? Result.faild("新增数据失败") : Result.ok();
    }
}
